﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GestionTaxesRiaApp.Web;
using System.ServiceModel.DomainServices.Client;


namespace GestionTaxesRiaApp.Views
{
    public partial class Connexion : Page
    {
        public static Utilisateur utilisateurStatic;
        GestionTaxesContext gestionTaxesContext = new GestionTaxesContext();
        public Connexion()
        {
            InitializeComponent();
        }



        string _currentText = "";
        void MotDePasseTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox __textBox = sender as TextBox;
            if (__textBox != null)
            {
                string __currentText = __textBox.Text;
                if (__currentText.Length < _currentText.Length)
                    _currentText = _currentText.Substring(0, __currentText.Length);
                if (__currentText != "")
                {
                    for (int i = 0; i < __currentText.Length; i++)
                    {
                        if (__currentText[i] != '\u25CF')
                        {
                            string __temp = __currentText.Remove(i, 1);
                            __textBox.Text = __temp.Insert(i, "\u25CF");
                            _currentText = _currentText.Insert
                    (_currentText.Length, __currentText[i].ToString());
                        }
                    }
                }
            }
        }
        

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void ConnexionBtn_Click(object sender, RoutedEventArgs e)
        {
            string motDepasse = _currentText;
            string login = LoginTB.Text;
            var utilisateur = gestionTaxesContext.Load<Utilisateur>(
            gestionTaxesContext.GetUtilisateurByLoginMotDePasseQuery(login, motDepasse),
            UserLoadedCallback, false);


        }
        void UserLoadedCallback(LoadOperation<Utilisateur> loadOperation)
        {
            var user = loadOperation.Entities.FirstOrDefault();
            if (user == null)
            {
                
                ErreurConnexionLbl.Visibility = System.Windows.Visibility.Visible;
                MotDePasseTB.Text = "";
                LoginTB.Text = "";
                
            }
            else
            {
                utilisateurStatic = user as Utilisateur;
                MotDePasseTB.Text = "";
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Gestions", UriKind.Relative));            }
            
        } 
    }
}
